package com.red.servlet.order;

import com.red.entity.User;
import com.red.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "OrderAddServlet", value = "/addOrder")

public class OrderAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain;charset=utf-8");

        int house_id = Integer.parseInt(req.getParameter("id"));
        User user = ((User) req.getSession().getAttribute("user"));

        switch (new OrderServiceImpl().createOrder(house_id, user.getuID())) {
            case 2:
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().print("禁止原地TP");
                break;
            case 1:
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().print("抢房失败，等级低于预定者");
                break;

            case 0:
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.getWriter().print("预定成功");
                break;
        }
    }
}
